package com.springbootdemo.controller;

import com.springbootdemo.pojo.User;
import com.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    public UserService userService;
    @Qualifier("stringRedisTemplate")
    @Autowired
    public RedisTemplate<String, String> redisTemplate;
    @RequestMapping("/getAllUser")
    public List<User> getAllUser(HttpServletResponse response, HttpServletRequest request){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("utf-8");
        redisTemplate.opsForValue().set("an","杨安华");
        String s = redisTemplate.opsForValue().get("an");
        System.out.println(s.toString());
        List<User> users = userService.selAllUser();
        return users;
    }
    @PostMapping("/getOne/{id}")
    public User getUser(HttpServletResponse response,  @PathVariable("id") int id){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("utf-8");
        User user = userService.selUserById(id);
        return user;
    }
}
